package com.jt.aop;

import com.jt.pojo.User;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Aspect
@Component
public class SpringAop {

    private static Map<Integer, Object> map = new HashMap();

    @Around("execution(* com.jt.service..*.*(..))")
    public Object around(ProceedingJoinPoint joinPoint) {
        Object result = null;
        System.out.println("环绕通知");
        Object[] args = joinPoint.getArgs();
        User user = (User) args[0];
        int id = user.getId();
        if (map.containsKey(id)){
            System.out.println("缓存获取");
            return map.get(user.getId());
        }else {
            try {
                result = joinPoint.proceed();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
            System.out.println("存入缓存");
            map.put(id, user);
        }
        return result;
    }
}
